
from src.command.basecommand import BaseCommand as bc
import src.constants as const
import src.utilities as utilities
from src.room import Room
from src.world import World

# All classes in this module should be in alphabetical order

class CreateRoom(bc):
  """build"""
  def action(self):

    print "Building a new room..."
    if self.room == None:
      self.sendMsg("You are nowhere", "player")
      return
    x, y, z = self.player.location

    typed_direction = self.args[0]
    direction = const.DIRECTION_SHORTCUT.get(typed_direction)
    if not direction:
      self.sendMsg("You didn't enter a valid direction", "player")
      return
  
    if direction == "north":       y += 1
    elif direction == "south":     y -= 1
    elif direction == "east":      x += 1
    elif direction == "west":      x -= 1
    elif direction == "northeast": y += 1 ; x += 1
    elif direction == "southeast": y -= 1 ; x += 1
    elif direction == "southwest": y -= 1 ; x -= 1
    elif direction == "northwest": y += 1 ; x -= 1
    elif direction == "up":        z += 1
    elif direction == "down":      z -= 1
    coords = (x, y, z)
    if str(coords) in self.player.server.world.rooms.keys():
      self.player.error("You're trying to build over a room that already "
                        "exists. If you would like to proceed, type 'link', "
                        "otherwise press <enter>.")
      link = self.player.input()
      if link == None: return
      if link == 'link':
        self.room.exits[direction] = coords
        new_direction = const.DIRECTION_OPPOSITE[direction]
        World.rooms.get(coords).exits[new_direction] = (self.room.coords)
      else:
        self.player.error("Aborting build!")
    else:
      new_room = Room(coords = coords)
      self.room.exits[direction] = new_room.coords
      new_room.exits[const.DIRECTION_OPPOSITE[direction]] = self.room.coords
      new_room.name = str(new_room.coords)
      new_room.desc = "# No Description #\n# No Description #\n# No Description #"


class ReloadCommands(bc):
  """_reload"""
  def action(self):

    self.player.reloadCmds()
    self.sendMsg("Commands reloaded!", "player")


class ShowUID(bc):
  """uid"""
  def action(self):

    if self.args:
      obj = utilities.getByName(self.args[0])
      if obj:
        self.sendMsg("{}: {}".format(obj.name, obj.UID), "player")
      else:
        self.sendMsg("There is nothing by that name.", "player")
    else:
      self.sendMsg("{}: {}".format(self.player.name, self.player.UID), "player")


